package com.huluobo.a2109mvpimdemo.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.huluobo.a2109mvpimdemo.R;
import com.huluobo.a2109mvpimdemo.chat.ChatActivity;
import com.huluobo.a2109mvpimdemo.chat.MyChatActivity;
import com.hyphenate.chat.EMConversation;
import com.hyphenate.chat.EMMessage;
import com.hyphenate.chat.EMTextMessageBody;
import com.hyphenate.util.DateUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by LC on 2023/11/24.
 */
public class ConversationAdapter extends RecyclerView.Adapter<ConversationAdapter.ConversationListItemViewHolder> {
    private List<EMConversation> conversations;
    private Context context;

    public ConversationAdapter(List<EMConversation> conversations, Context context) {
        this.conversations = conversations;
        this.context = context;
    }

    @NonNull
    @Override
    public ConversationListItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ConversationListItemViewHolder(LayoutInflater
                .from(parent.getContext()).inflate(R.layout.view_conversation_item, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ConversationListItemViewHolder holder, int position) {
        //会话列表的用户名
        holder.username.setText(conversations.get(position).conversationId());
        //最后一条消息
        if (conversations.get(position).getLastMessage().getType() == EMMessage.Type.TXT) {
            EMTextMessageBody body = (EMTextMessageBody) conversations.get(position).getLastMessage().getBody();
            holder.lastMessage.setText(body.getMessage());
        } else {
            holder.lastMessage.setText("非文本消息");
        }
        //最后一条消息的时间戳
        String timestampString =
                DateUtils.getTimestampString(new Date(conversations.get(position).getLastMessage().getMsgTime()));
        holder.timestamp.setText(timestampString);
        //未读消息数量
        if (conversations.get(position).getUnreadMsgCount() > 0) {
            holder.unreadCount.setVisibility(View.VISIBLE);
            holder.unreadCount.setText(conversations.get(position).getUnreadMsgCount() + "");
        } else {
            holder.unreadCount.setVisibility(View.GONE);
        }

        holder.itemView.setOnClickListener(v -> {
            int chatType = 0;
            switch (conversations.get(position).getType()) {
                case Chat:
                    chatType = 1;
                    break;
                case GroupChat:
                    chatType = 2;
                    break;
            }
            String conversationId = conversations.get(position).conversationId();
            Intent intent = new Intent(context, ChatActivity.class);
            Bundle bundle = new Bundle();
            bundle.putString("conversationId", conversationId);
            bundle.putInt("chatType", chatType);
            intent.putExtras(bundle);
            context.startActivity(intent);
        });
    }

    @Override
    public int getItemCount() {
        return conversations.size();
    }

    class ConversationListItemViewHolder extends RecyclerView.ViewHolder {
        TextView username, lastMessage, timestamp, unreadCount;

        public ConversationListItemViewHolder(@NonNull View itemView) {
            super(itemView);
            username = itemView.findViewById(R.id.username);
            lastMessage = itemView.findViewById(R.id.lastMessage);
            timestamp = itemView.findViewById(R.id.timestamp);
            unreadCount = itemView.findViewById(R.id.unreadCount);
        }
    }
}
